﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace LdCms.BLL.Community
{
    using LdCms.EF.DbEntitiesContext;
    using LdCms.EF.DbModels;
    using LdCms.EF.DbStoredProcedure;
    using LdCms.IBLL.Community;
    using LdCms.IDAL.Community;
    using LdCms.Common.Json;
    using LdCms.Common.Security;
    using LdCms.Common.Extension;
    public partial class ColumnPermissionService:BaseService<Ld_Community_ColumnPermission>,IColumnPermissionService
    {
        private readonly IColumnPermissionDAL ColumnPermissionDAL;
        private readonly LdCmsDbEntitiesContext LdCmsDbEntitiesContext;
        public ColumnPermissionService(LdCmsDbEntitiesContext LdCmsDbEntitiesContext, IColumnPermissionDAL ColumnPermissionDAL)
        {
            this.LdCmsDbEntitiesContext = LdCmsDbEntitiesContext;
            this.ColumnPermissionDAL = ColumnPermissionDAL;
            this.Dal = ColumnPermissionDAL;
        }
        public override void SetDal()
        {
            Dal = ColumnPermissionDAL;
        }

        public bool VerifyColumnPermission(int systemId, string companyId, string columnId, string memberId, int typeId)
        {
            try
            {
                bool result = false;
                int intnum = 0;
                var dbContext = new DAL.BaseDAL(LdCmsDbEntitiesContext);
                var db = dbContext.DbEntities();
                var trans = db.Database.BeginTransaction();
                try
                {
                    var expression = ExtLinq.True<Ld_Member_Account>();
                    expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId && m.MemberID == memberId);
                    var entityMember = dbContext.Find(expression);
                    if (entityMember != null)
                    {
                        string rankId = entityMember.RankID;
                        result = dbContext.IsExists<Ld_Community_ColumnPermission>(m => m.SystemID == systemId && m.CompanyID == companyId && m.ColumnID == columnId && m.TypeID == typeId && m.RankID == rankId);
                    }
                    intnum = db.SaveChanges();
                    trans.Commit();
                }
                catch (Exception)
                {
                    trans.Rollback();
                }
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public List<Ld_Community_ColumnPermission> GetColumnPermissionByColumnId(int systemId, string companyId, string columnId)
        {
            try
            {
                var expression = ExtLinq.True<Ld_Community_ColumnPermission>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId && m.ColumnID == columnId);
                return FindList(expression, m => m.CreateDate, false).ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public List<Ld_Community_ColumnPermission> GetColumnPermissionByColumnId(int systemId, string companyId, string columnId,int typeId)
        {
            try
            {
                var expression = ExtLinq.True<Ld_Community_ColumnPermission>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId && m.ColumnID == columnId && m.TypeID == typeId);
                return FindList(expression, m => m.CreateDate, false).ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

    }
}
